package univer.oko.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author Eduard Dudar
 *         DATE: 17.08.2005
 *         Copyright 2005
 */
public class CDDialog extends JDialog {
    protected JPanel contentPane;
    private CDButton buttonOK;
    private CDButton buttonCancel;

    public CDDialog() {
        this.setContentPane(contentPane);
        this.setModal(true);
        this.getRootPane().setDefaultButton(buttonOK);

        this.initLayout();
        this.initActions();
    }

    protected void initLayout() {
    }

    private void initActions() {
        this.buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        this.buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        this.contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    }

    protected void setPanel(JPanel panel) {
        this.contentPane.add(panel, BorderLayout.CENTER);
    }

    protected void onOK() {
        dispose();
    }

    protected void onCancel() {
        dispose();
    }
}
